.TH std::variant_size,std::variant_size_v 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::variant_size,std::variant_size_v \- std::variant_size,std::variant_size_v

.SH Synopsis
   Defined in header <variant>
   template< class T >                                        \fB(1)\fP \fI(since C++17)\fP
   struct variant_size; /* undefined */
   template< class... Types >

   struct variant_size<std::variant<Types...>>                \fB(2)\fP \fI(since C++17)\fP

       : std::integral_constant<std::size_t,
   sizeof...(Types)> {};
   template< class T >                                        \fB(3)\fP \fI(since C++17)\fP
   class variant_size<const T>;
   template< class T >                                        \fB(4)\fP \fI(since C++17)\fP
   class variant_size<volatile T>;                                (deprecated in C++20)
   template< class T >                                        \fB(5)\fP \fI(since C++17)\fP
   class variant_size<const volatile T>;                          (deprecated in C++20)

   Provides access to the number of alternatives in a possibly cv-qualified variant as
   a compile-time constant expression.

   Formally,

   2) meets the UnaryTypeTrait requirements with a base characteristic of
   std::integral_constant<std::size_t, sizeof...(Types)>
   3-5) meets the UnaryTypeTrait requirements with a base characteristic of
   std::integral_constant<std::size_t, std::variant_size<T>::value>

.SH Member constants

   value    sizeof...(Types)
   \fB[static]\fP \fI(public static member constant)\fP

.SH Member functions

   operator std::size_t converts the object to std::size_t, returns value
                        \fI(public member function)\fP
   operator()           returns value
   \fI(C++14)\fP              \fI(public member function)\fP

.SH Member types

   Type       Definition
   value_type std::size_t
   type       std::integral_constant<std::size_t, value>

.SH Notes

   All specializations of std::variant_size satisfy UnaryTypeTrait with base
   characteristic std::integral_constant<std::size_t, N> for some N.

.SH Example


// Run this code

 #include <any>
 #include <variant>

 static_assert(std::variant_size_v<std::variant<>> == 0);
 static_assert(std::variant_size_v<std::variant<int>> == 1);
 static_assert(std::variant_size_v<std::variant<int, int>> == 2);
 static_assert(std::variant_size_v<std::variant<int, int, int>> == 3);
 static_assert(std::variant_size_v<std::variant<int, float, double>> == 3);
 static_assert(std::variant_size_v<std::variant<std::monostate, void>> == 2);
 static_assert(std::variant_size_v<std::variant<const int, const float>> == 2);
 static_assert(std::variant_size_v<std::variant<std::variant<std::any>>> == 1);

 int main() {}

.SH See also

   variant_alternative         obtains the type of the alternative specified by its
   variant_alternative_t       index, at compile time
   \fI(C++17)\fP                     \fI(class template)\fP (alias template)
   std::tuple_size<std::tuple> obtains the size of tuple at compile time
   \fI(C++11)\fP                     \fI(class template specialization)\fP
